<?php

namespace app\api\controller;

use app\api\controller\Base;

/**
 * 矿工接口
 */
class Miners extends Base
{
    /**
     * 矿工页面
     */
    public function index(){
        $strength = $this->user['strength'];
        $this->success('操作成功',$strength);
    }
    /**
     * 矿工商店
     * 
     */
    public function shop()
    {
        $list = db('miners')->field('id,image,name,length,price,production_jin,production_yin,limit')->order('sort')->select();
        foreach($list as $k=>$v){
            $list[$k]['ability'] = ceil(($v['production_jin']+$v['production_yin'])/$v['length']);
        }

        $this->success('操作成功',$list);
    }
    /**
     *  购买矿工
     */
    public function buyminers(){
        // $this->test();die;
        //矿工id
        $id = input('id');
        if(!$id){
            $this->error('矿工id不能空');
        }
        //支付密码
        $paypass = input('paypass');
        if(!$paypass){
            $this->error('支付密码不能空');
        }
        $miners = db('miners')->where(['id'=>$id])->find();
        if(!$miners){
            $this->error('矿工不存在');
        }
        //支付密码是否正确
        if($this->user['paypass']!=md5($paypass)){
            $this->error('支付密码不正确');
        }
        //判断是否达到限制
        $count = db('user_miners')->where(['user_id'=>$this->user['id'],'miners_id'=>$id])->count();
        if($count>=$miners['limit']){
            $this->error('该矿工达到上限');
        }
        //判断金额是否足够
        if($this->user['gold']<$miners['price']){
            $this->error('金币不足');
        }
        //扣钱
        $r = db('user')->where(['id'=>$this->user['id']])->setDec('gold',$miners['price']);
        //加入财务日志
        $this->addfinancial($this->user['id'],1,'购买矿工：'.$miners['name'],7,$miners['price']);
        //加到我的矿工表
        $data['user_id'] = $this->user['id'];
        $data['miners_id'] = $id;
        $data['buy_time'] = time();
        $data['end_time'] = time()+$miners['length']*24*60*60;
        $res = db('user_miners')->insert($data);
        //增加推广奖励
        $this->reward($this->user['id'],$id);
        //赠送上级无报废矿机
        $this->noscrap($this->user['id'],$id);
        $this->success('购买成功');
    }
    /**
     * 推广奖励
     */
    public function reward($user_id,$miners_id){
        $user = db('user')->where(['id'=>$user_id])->find();
        $miners = db('miners')->where(['id'=>$miners_id])->find();
        //一级奖励
        $first = $miners['price']*config('site.first');
        if($user['first']){
            db('user')->where(['id'=>$user['first']])->setInc('strength',$first);
            $this->addfinancial($user['first'],2,'用户：'.$user_id.'一级推荐奖励',8,0,0,$first);
        }
        //二级奖励
        $second = $miners['price']*config('site.second');
        if($user['second']){
            db('user')->where(['id'=>$user['second']])->setInc('strength',$second);
            $this->addfinancial($user['second'],2,'用户：'.$user_id.'二级推荐奖励',8,0,0,$second);
        }
        //三级奖励
        $third = $miners['price']*config('site.third');
        if($user['third']){
            db('user')->where(['id'=>$user['third']])->setInc('strength',$third);
            $this->addfinancial($user['third'],2,'用户：'.$user_id.'三级推荐奖励',8,0,0,$third);
        }
    }
    /**
     * 赠送上级无报废矿机
     */
    public function noscrap($user_id,$miners_id){
        $user = db('user')->where(['id'=>$user_id])->find();
        $miners = db('miners')->where(['id'=>$miners_id])->find();
        //判断推荐人是否有此矿机
        $where['status'] = ['in',[1,3]];
        $r = db('user_miners')->where(['user_id'=>$user['first'],'miners_id'=>$miners_id])->where($where)->find();
        if(!$r){
            return false;
        }
        //找此直推人的所有下级
        $first = db('user')->field('id')->where(['first'=>$user['first']])->select();
        if(count($first)<config('site.tui_num')){
            return false;
        }
        $user_id = [];
        foreach($first as $k=>$v){
            $user_id[] = $v['id'];
        }
        $where['user_id'] = ['in',$user_id];
        $where['miners_id'] = $miners_id;
        $res = db('user_miners')->where($where)->group('user_id')->count();
        if($res>config('site.tui_num')){
            //赠送一台无报废矿机
            $data['user_id'] = $user['first'];
            $data['miners_id'] = $miners_id;
            $data['buy_time'] = time();
            $data['end_time'] = time()+99999999999;
            $data['type'] = 3;
            db('user_miners')->insert($data);
        }
    }
    /**
     * 赠送自己无报废矿机
     */
    public function mynoscrap($user_id,$miners_id){
        $user = db('user')->where(['id'=>$user_id])->find();
        $miners = db('miners')->where(['id'=>$miners_id])->find();
        //找此自己的所有下级
        $first = db('user')->field('id')->where(['first'=>$user_id])->select();
        if(count($first)<config('site.tui_num')){
            return false;
        }
        $user_id = [];
        foreach($first as $k=>$v){
            $user_id[] = $v['id'];
        }
        $where['user_id'] = ['in',$user_id];
        $where['miners_id'] = $miners_id;
        $res = db('user_miners')->where($where)->group('user_id')->count();
        if($res>config('site.tui_num')){
            //赠送自己一台无报废矿机
            $data['user_id'] = $user_id;
            $data['miners_id'] = $miners_id;
            $data['buy_time'] = time();
            $data['end_time'] = time()+99999999999;
            $data['type'] = 3;
            db('user_miners')->insert($data);
        }
    }
    /**
     * 签到启动
     */
    public function signin(){
        // // 用户旷工id
        // $id = input('id');
        // if(!$id){
        //     $this->error('参数错误');
        // }
        // $user_miners = db('user_miners')->where(['id'=>$id])->find();
        //判断矿工是否过期
        // if($user_miners['status']=99){
        //     $this->error('该矿工已过期');
        // }
        // //判断矿工今天是否已签到
        // if($user_miners['type']!=1){
        //     $this->error('该矿工已签到');
        // }
        $where['user_id'] = $this->user['id'];
        $where['status'] = ['in','1,3'];
        $where['type'] = 1;
        $list = db('user_miners')->where($where)->select();
        if(empty($list)){
            $this->error('矿工已经签到');
        }
        $data['type'] = 2;
        $data['start_time1'] = time();
        $r = db('user_miners')->where($where)->update($data);
        $this->success('签到成功');
    }
    /**
     * 加速
     */
    public function addspeed(){
        // 用户旷工id
        $id = input('id');
        if(!$id){
            $this->error('参数错误');
        }
        $user_miners = db('user_miners')->where(['id'=>$id])->find();
        //判断矿工是否过期
        if($user_miners['status']==99){
            $this->error('该矿工已过期');
        }
        if($user_miners['type']<3){
            $this->error('该矿工未签到或正在产币');
        }
        //判断矿工今天是否已加速
        if($user_miners['type']>3){
            $this->error('该矿工已加速');
        }
        $miners = db('miners')->where(['id'=>$user_miners['miners_id']])->find();
        //查看体力是否够用
        if($this->user['strength']<$miners['strength']){
            $this->error('体力不足');
        }
        //扣除体力
        db('user')->where(['id'=>$this->user['id']])->setDec('strength',$miners['strength']);
        $this->addfinancial($this->user['id'],1,'加速矿工消耗体力',6,0,0,$miners['strength']);
        //更改状态
        $data['type'] = 4;
        $data['start_time2'] = time();
        $data['start_time1'] = time();
        $r = db('user_miners')->where(['id'=>$id])->update($data);
        $this->success('加速成功');
    }
    /**
     * 矿工规则
     */
    public function minersrule(){
        $data = config('site.miners');
        $this->success('操作成功',$data);
    }
    /**
     * 矿工收益
     */
    public function earnings(){
        //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        //1 金币 2银币
        $type = input('type')?input('type'):1;
        $where['user_id'] = $this->user['id'];
        $where['status'] = 5;
        $count = db('user_log')->where($where)->order('createtime desc')->count();
        $list = db('user_log')->field('silver_num,glod_num,desc,status,createtime')->where($where)->order('createtime desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            if($type==1){
                $list[$k]['coin'] = $v['glod_num'];
            }else{
                $list[$k]['coin'] = $v['silver_num'];
            }
        }
        $data['count'] = $count;
        $data['list'] = $list;
        $this->success('操作成功',$data);

    }
    /**
     * 直推规则
     */
    public function firstDrive(){
        $data = config('site.first_drive');
        $this->success('操作成功',$data);
    }
    /**
     * 矿工详情
     */
    public function minersdetail(){
        $id = input('id');
        if(empty($id)){
            $this->error('参数错误');
        }
        $miners = db('miners')->where(['id'=>$id])->find();
        if(empty($miners)){
            $this->error('参数错误');
        }
        $data['need'] = $miners['strength'];
        $data['user_strength'] = $this->user['strength'];
        $this->success('操作成功',$data);
    }
    
}
